package com.bee.剑指offer;

import java.util.Locale;

/**
 * -325353312
 */
public class 把字符串转换成整数 {

    public static int StrToInt(String str) {
        str = str.trim();
        if(str.length() == 0) {
            return 0;
        }
        char[] chars = str.toCharArray();
        int i = 0;
        int flag = 1;
        if (chars[0] == '-' || chars[0] == '+') {
            i++;
            if(chars[0] == '-') { // 负数
                flag = -1;
            }
        }
        int ano = 0;
        for (; i < chars.length; i++) {
            if(chars[i] >= '0' && chars[i] <= '9') {
                int temp = chars[i] - '0';
                ano = ano*10 + temp;
            } else {
                return flag*ano;
            }
        }
        if(i == chars.length) {
            return flag*ano;
        }
        return 0;
    }

    public static void main(String[] args) {
        int i = StrToInt("2147483648");
        System.out.println(i);
    }
}
